package mr;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

//平均分8份
public class Demo2Split {
    public static void main(String[] args)throws Exception{
        int sum = sumFileLine();
        int fileRow = sum / 8; //切分

        int index = 0;
        BufferedReader br = new BufferedReader(new FileReader("E:\\IDEJAVA\\mapreduce\\data\\bigstudent.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\IDEJAVA\\mapreduce\\split\\split---" + index));


        String line;
        int flag=0;
        while ((line=br.readLine())!=null){
            flag++;
            if(flag>fileRow){ //先把原有的数据从缓冲区保存到文件中，然后生成新文件
                bw.flush();
                bw.close();
                index++;
                flag=0;
                bw= new BufferedWriter(new FileWriter("E:\\IDEJAVA\\mapreduce\\split\\split---" + index));
            }
            bw.write(line);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        br.close();
    }
    /**
     * 统计文件的行数
     *
    */
    public static int sumFileLine() throws Exception{
        BufferedReader br = new BufferedReader(new FileReader("E:\\IDEJAVA\\mapreduce\\data\\bigstudent.txt"));
        int sum=0;
        String line;
        while ((line=br.readLine())!=null){
            sum++;
        }
        br.close();
        return sum;
    }
}
